#------------------------help information
if [ -z $1 ] || [ -z $2 ] || [ ! -d $1 ] || [ ! -d $2 ]  ;
then
	echo "How to use:"
	echo "		$(basename $0)  'folder1' 'folder2' "
	echo "		 diff files under 2 folders" 
	exit 0
fi


#------------------------log file
LogFile=DiffFD2FDLog
function LogFile
{
	bLog=0
	Num=1
	while [ -f $LogFile$Num ]
	do
		Num=$[ $Num +1]
	done
	LogFile=$LogFile$Num
	echo "create log file $LogFile"
	touch $LogFile #create the log file
	echo "------------------$(date) $(basename $0) " >> $LogFile
}


function IsFiltedFiles
{
	f1=$1
	baseF1=$(basename $f1)
        extenF1=${baseF1##*.}
#0 means not to filter

	if [ -d $f1 -o $baseF1 = "Makefile"  ];
	then
		return 0;
	fi

	if [ "$extenF1" = "cpp"  -o  "$extenF1" = "hpp"  -o  "$extenF1" = "c"  -o  "$extenF1" = "h"  -o  "$extenF1" = "mk"  -o  "$extenF1" = "htm"  -o  "$extenF1" = "html" -o  "$extenF1" = "txt"  ];
	then
		return 0;
	fi

	return 1;
}

count=0
#------------------------Diff folders
function DiffFolder
{
	fld1=$1
	fld2=$2
	temp=""
	echo "-----------------DifferFolder $fld1 $fld2"
	for f1 in $fld1/*;
	do 
		#  filtered files not need to be checked
		IsFiltedFiles $f1
		if [ $? -eq 1 ];
		then
			echo "SKIP comparing $f1"
			continue;
		fi

		echo "$f1"
		for f2 in $fld2/*;
		do	
			#                        echo "comparing $f1 $f2"
			baseF1=$(basename $f1)
			baseF2=$(basename $f2)
			
#                        echo "   compare $f1 $baseF2"	

			if [ "$baseF1" = "$baseF2" ];
			then 
				if [ -f $f1 -a -f $f2 ]; # condition : same files
				then
					count=$[ $count+1 ]
					# check if file content same
					temp=$(diff $f1 $f2);
					if [ -n "$temp" ];
					then
						if [ -f $LogFile ];
						then

							echo "	!!!! diff $f1 $f2"
							echo "diff $f1 $f2">>$LogFile
						else
							echo "	!!!!NOLOG: diff $f1 $f2"
						fi
						break;
					fi	  	
				fi

				if [ -d $f1 -a -d $f2 ]; # condition: same folders
				then
					DiffFolder $f1 $f2	
					break;
				fi
			fi

		done;
	done
echo "***Total compared common files:$count: sub folder $1"	
}

LogFile
fld1=$1
fld2=$2
DiffFolder $fld1 $fld2
echo "***Total compared common files:$count"	

